﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using System.Configuration;

namespace SMP.Client
{
    using SMP.Resource;
    using SMP.Service;
    public partial class FrmMain : RibbonForm
    {
        Vaitro_Service _objVaitro = new Vaitro_Service();
        public static DataTable dtb_thanhvien;
        public static bool check_login = false;
        string _loginString = "Đăng nhập";
        string _logoutString = "Đăng xuất";

        public FrmMain()
        {
            InitializeComponent();
            InitSkinGallery();
            ChangeStatusTab(false);            
        }
        void InitSkinGallery()
        {
            SkinHelper.InitSkinGallery(rgbiSkins, true);
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            var sql = new SQLServer();
            if (!sql.CheckDbExists(SQLServer.DbName))
                bbiCaidatDB_ItemClick(sender, null);

            bbiDangnhap_ItemClick(sender, null);
        }

        private void ChangeStatusTab(bool istrue = true)
        {
            tab_Trangchu.Visible = istrue;
            tab_Nhapxuat.Visible = istrue;
            tab_Thongke.Visible = istrue;

            rPage_Vaitro.Visible = istrue;
            rPage_Mathang.Visible = istrue;
            rPage_Nhapxuat.Visible = istrue;
            rPage_Hangban.Visible = istrue;
            rPage_Hangnhap.Visible = istrue;

            var action = istrue == true ? BarItemVisibility.Always : BarItemVisibility.Never;
            bbi_FrmInput_Dulieu.Visibility = action;
            bbi_FrmVaitro.Visibility = action;
            bbi_FrmThanhvien.Visibility = action;
            bbi_FrmMathang.Visibility = action;
            bbi_FrmNhapkho.Visibility = action;
            bbi_FrmXuatkho.Visibility = action;
            bbi_FrmThongke_Hangban.Visibility = action;
            bbi_FrmThongke_Hangnhap.Visibility = action;

        }

        #region Trang chủ

        private void bbiCaidatDB_ItemClick(object sender, ItemClickEventArgs e)
        {
            var frm = new SystemForm.FrmSetting();
            frm.ShowDialog();
        }

        private void bbiNguoidung_ItemClick(object sender, ItemClickEventArgs e)
        {
            //MessageBox.Show("Chức năng đang được phát triển", "Thông tin...");
            foreach (var x in MdiChildren) if (x is PolForm.FrmThanhvien) return;
            var frm = new PolForm.FrmThanhvien() { MdiParent = this };

            frm.Show();
        }

        private void bbiQuyen_ItemClick(object sender, ItemClickEventArgs e)
        {
            //MessageBox.Show("Chức năng đang được phát triển", "Thông tin...");
            foreach (var x in MdiChildren) if (x is PolForm.FrmVaitro) return;
            var frm = new PolForm.FrmVaitro() { MdiParent = this };

            frm.Show();
        }

        private void bbiDangnhap_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (bbiDangnhap.Caption == _logoutString)
            {
                check_login = false;
                dtb_thanhvien = null;
                bbiDangnhap.Caption = _loginString;
                bbiDong_ItemClick(sender, e);
                ChangeStatusTab(false);
            }

            var frm = new SystemForm.FrmLogin();
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.ShowDialog();

            if (check_login)
            {
                bbiDangnhap.Caption = _logoutString;
                CheckRole(dtb_thanhvien.Rows[0]["Id_Thanhvien"]);
                sttInfo.Caption = dtb_thanhvien.Rows[0]["Hoten"] + "";
            }
            else
                this.Close();
            
        }

        private void bbiDong_ItemClick(object sender, ItemClickEventArgs e)
        {
            foreach (var frm in MdiChildren) frm.Close();
        }

        private void iExit_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.Close();
        }

        #endregion


        #region Nhập - Xuất

        private void bbiMathang_ItemClick(object sender, ItemClickEventArgs e)
        {
            foreach (var x in MdiChildren) if (x is Form.FrmMathang) return;
            var frm = new Form.FrmMathang() { MdiParent = this };

            frm.Show();
        }

        private void bbiNhaphang_ItemClick(object sender, ItemClickEventArgs e)
        {
            foreach (var x in MdiChildren) if (x is Form.FrmNhapkho) return;
            var frm = new Form.FrmNhapkho() { MdiParent = this };

            frm.Show();
        }

        private void bbiXuathang_ItemClick(object sender, ItemClickEventArgs e)
        {
            foreach (var x in MdiChildren) if (x is Form.FrmXuatkho) return;
            var frm = new Form.FrmXuatkho() { MdiParent = this };

            frm.Show();
        }

        #endregion


        #region Thống kê

        private void bbiHangban_Theongay_ItemClick(object sender, ItemClickEventArgs e)
        {
            foreach (var x in MdiChildren) if (x is Form.FrmThongke_Hangban) return;
            var frm = new Form.FrmThongke_Hangban() { MdiParent = this };

            frm.Show();
        }

        private void bbiHangnhap_Theongay_ItemClick(object sender, ItemClickEventArgs e)
        {
            foreach (var x in MdiChildren) if (x is Form.FrmThongke_Hangnhap) return;
            var frm = new Form.FrmThongke_Hangnhap() { MdiParent = this };

            frm.Show();
        }

        private void bbiHangton_ItemClick(object sender, ItemClickEventArgs e)
        {
            //foreach (var x in MdiChildren) if (x is Form.FrmThongke_Hangban) return;
            //var frm = new Form.FrmThongke_Hangban() { MdiParent = this };

            //frm.Show();
            MessageBox.Show("Chức năng đang được phát triển", "Thông tin...");
        }



        #endregion


        #region Method

        private DataTable GetSchema_Menu()
        {
            var dtb = new DataTable("Menu");
            dtb.Columns.Add("Id", typeof(int));
            dtb.Columns.Add("Parent", typeof(int));
            dtb.Columns.Add("Name", typeof(int));

            dtb.Rows.Add("1", "0", "tab_Trangchu");
            dtb.Rows.Add("2", "0", "tab_Nhapxuat");
            dtb.Rows.Add("3", "0", "tab_Thongke");

            dtb.Rows.Add("4", "1", "rPage_Caidat");
            dtb.Rows.Add("5", "1", "rPage_Vaitro");

            dtb.Rows.Add("6", "2", "rPage_Mathang");
            dtb.Rows.Add("7", "2", "rPage_Nhapxuat");

            dtb.Rows.Add("8", "3", "rPage_Hangban");
            dtb.Rows.Add("9", "3", "rPage_Hangnhap");

            dtb.Rows.Add("10", "4", "FrmCaidat_Dulieu");
            dtb.Rows.Add("11", "5", "FrmThanhvien");
            dtb.Rows.Add("12", "5", "FrmVaitro");            

            dtb.Rows.Add("13", "6", "FrmMathang");
            dtb.Rows.Add("14", "7", "FrmNhapkho");
            dtb.Rows.Add("15", "7", "FrmXuatkho");
            dtb.Rows.Add("16", "8", "FrmThongke_Hangban");
            dtb.Rows.Add("17", "9", "FrmThongke_Hangnhap");
            
            return dtb;
        }

        private void CheckRole(object id_thanhvien)
        {
            if (id_thanhvien + "" == "")
                return;
            var dtb = _objVaitro.GetAll_Form_By_Id_Thanhvien(id_thanhvien);
            if (dtb == null || dtb.Rows.Count == 0)
            {
                Windows.Forms.Message.Show("Tài khoản của bạn chưa được cấp quyền.\nVui lòng liên hệ Administrator để được cấp quyền truy cập", "Thông Báo");
                ChangeStatusTab(false);
                return;
            }
            else
            {
                int i = 0, j = 0;
                ChangeStatusTab();
                BarButtonItem[] _array = new BarButtonItem[] { 
                    bbi_FrmVaitro, bbi_FrmThanhvien, bbi_FrmMathang, bbi_FrmNhapkho, bbi_FrmXuatkho, bbi_FrmThongke_Hangban, bbi_FrmThongke_Hangnhap };
                foreach(BarButtonItem bbi in _array)
                {
                    var name = bbi.Name.Substring(4);
                    DataRow[] row = dtb.Select(string.Format("Ten_Form = '{0}'", name));
                    if (row.Length == 0)
                    {
                        bbi.Visibility = BarItemVisibility.Never;
                        switch (name)
                        {
                            case "FrmMathang":
                                rPage_Mathang.Visible = false;
                                break;
                            case "FrmThongke_Hangban":
                                rPage_Hangban.Visible = false;
                                break;
                            case "FrmThongke_Hangnhap":
                                rPage_Hangnhap.Visible = false;
                                break;
                            case "FrmXuatkho":
                                i++;
                                break;
                            case "FrmNhapkho":
                                i++;
                                break;
                            case "FrmVaitro":
                                j++;
                                break;
                            case "FrmThanhvien":
                                j++;
                                break;
                        }
                        if (i == 2)
                            rPage_Nhapxuat.Visible = false;
                        if (j == 2)
                            rPage_Vaitro.Visible = false;
                    }

                }
            }
        }

        
        #endregion

        

   

















    }
}